package com.nextlevel;

import javax.xml.crypto.Data;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Date;

import static com.nextlevel.GameUtil.*;

public class MyGameFrame extends Frame {
    //放在窗口中，因为加载一次就可以
    Image bgImg = GameUtil.getImage("images/bg.jpg");
    Image planeImg = GameUtil.getImage("images/plane.png");

    Plane plane = new Plane(planeImg,100,400,7);

    Shell[] shells = new Shell[50];

    Explode explode;

    Date startTime = new Date();
    Date endTime;
    int period;

    //初始化窗口
    public void launchFrame(){
        this.setTitle("飞机大作战");
        this.setVisible(true);  //  窗口默认不可见，需要可视。
        this.setSize(FRAME_WIDTH,FRAME_HEIGHT);
        this.setLocation(500,200);

        //增加关闭窗口的动作,匿名内部类，适配器
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0); //退出程序
            }
        });

        //启动窗口，绘制线程
        new PaintThread().start();

        //启动键盘监听
        this.addKeyListener(new KeyMonitor());

        //初始化50发炮弹
        for(int i=0;i<shells.length;i++){
            shells[i] = new Shell();
        }

    }


    //绘制游戏画面
    @Override
    public void paint(Graphics g) {
        //绘制背景
        g.drawImage(bgImg,0,0,FRAME_WIDTH,FRAME_HEIGHT,null);

        //绘制飞机
        plane.drawMySelf(g);

        //绘制炮弹
        for (int i = 0;i<shells.length;i++) {
            if (shells[i]!=null) {
                shells[i].drawMySelf(g);

                boolean crash = shells[i].getRec().intersects(plane.getRec());

                if(crash){
                    plane.live = false;

                    endTime = new Date();

                    period = (int)((endTime.getTime()-startTime.getTime())/1000);
                    if(explode==null){
                        explode = new Explode(plane.x, plane.y);
                    }
                    explode.draw(g);
                }
            }
        }
        if (!plane.live) {
            g.setColor(Color.BLUE);
            Font f = new Font("宋体", Font.BOLD,20);
            g.setFont(f);
            g.drawString("时间：" + period + "秒", 200, 250);
        }
    }

    //键盘监听内部类
    class KeyMonitor extends KeyAdapter {

        //重写KeyAdapter中的两个方法

        //按下键
        @Override
        public void keyPressed(KeyEvent e) {
            plane.addDirection(e);
        }

        //抬起键
        @Override
        public void keyReleased(KeyEvent e) {
            plane.minusDirection(e);
        }
    }

    //多线程和内部类实现动画
    //重画线程
    class PaintThread extends Thread{
        @Override
        public void run() {
            //固定写法，死循环
            while (true){
                repaint();
                try {
                    Thread.sleep(40);   //40ms
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    public static void main(String[] args) {
        MyGameFrame frame = new MyGameFrame();
        frame.launchFrame();
    }

    //双缓冲技术
    private Image offScreenImage = null;
    public void update(Graphics g){
        if (offScreenImage == null){
            offScreenImage = this.createImage(FRAME_WIDTH,FRAME_HEIGHT);
        }

        Graphics gOff = offScreenImage.getGraphics();
        paint(gOff);
        g.drawImage(offScreenImage,0,0,null);
    }



}
